package com.newegg.research.DecisionTree;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.logging.Logger;

import org.apache.commons.io.FileUtils;

public class PreAnalysisData
{
	static Logger logger = Logger.getLogger(PreAnalysisData.class.getName()); 
	
	public void analysis(String filepath) throws IOException
	{
		File analysis_file = new File(filepath);
		Iterator<String> it = FileUtils.lineIterator(analysis_file);
		while(it!=null && it.hasNext())
		{
			String line = it.next().replaceAll("(\\s\\s)+", " ");
			int split_point = line.indexOf(",");
			if(split_point < 1)
			{
				logger.warning("The line is bad formatted:" + line);
			}
			else
			{
				String name = line.substring(0,split_point).replaceAll("[(^\\s)|(\\s$)]*", "");
				String value = line.substring(split_point + 1).replaceAll("[(^\\s)|(\\s$)]*", "");
				Feature feature = Feature.getFeature(name);
				feature.addValue(value);
			}
		}
	}
	
	
	
	public static void main(String[] args)
	throws Exception
	{
		PreAnalysisData pad = new PreAnalysisData();
		logger.info("Analyzing... ");
		pad.analysis("data/feature_mapping.txt");
		logger.info("Write object to file");
		pad.outputResult();
//		Feature.writeMap("data/tempAnalysis.object");
	}
	
	public void outputResult() throws IOException
	{
		Collection c = Feature.features.values();
		Iterator<Feature> iter = c.iterator();
		BufferedWriter bw = new BufferedWriter(new FileWriter("data/analysis_result.txt",true));
		final String sperator = "$";
		while(iter.hasNext())
		{
			Feature f = iter.next();
			String name = f.name;
			Set<Entry<String, Integer>> entryset = f.count.entrySet();
			Iterator<Entry<String, Integer>> it = entryset.iterator();
			while(it.hasNext())
			{
				Entry<String, Integer> entry = it.next();
				String value = entry.getKey();
				Integer count = entry.getValue();
				bw.append(name);
				bw.append(sperator);
				bw.append(value);
				bw.append(sperator);
				bw.append(count+"");
				bw.append("\n");
			}
		}
		bw.close();
	}
	
	
	private static class Feature implements Serializable
	{
		public static void writeMap(String filepath) throws FileNotFoundException, IOException
		{
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filepath));
			oos.writeObject(features);
			oos.close();
		}
		
		static Map<String, Feature> features = new HashMap<String, Feature>();
		
		Map<String, Integer> count = new HashMap<String, Integer>();
		
		public static Feature getFeature(String name)
		{
			if(features.containsKey(name))
				return features.get(name);
			else
			{
				Feature f = new Feature(name);
				features.put(name, f);
				return f;
			}
		}
		private static final long serialVersionUID = -705949004865426551L;
		String name;
		Feature(String name)
		{
			this.name = name;
		}
		public void addValue(String value)
		{
			if(count.containsKey(value))
			{
				count.put(value, count.get(value)+1);
			}else
			{
				count.put(value, 1);
			}
		}
		@Override
		public int hashCode()
		{
			return name.hashCode();
		}
	}
}
